package cn.lg.soar.system.biz.modules.tenant.controller;

import cn.lg.soar.common.model.HttpResult;
import cn.lg.soar.common.model.PageQuery;
import cn.lg.soar.common.model.PageResult;
import cn.lg.soar.core.annotations.IdempotentByParam;
import cn.lg.soar.database.base.controller.BaseController;
import cn.lg.soar.database.query.ConditionEnum;
import cn.lg.soar.database.query.QueryParams;
import cn.lg.soar.mvc.annotations.*;
import cn.lg.soar.common.enums.LogTypeEnum;
import cn.lg.soar.system.api.constant.ModuleConst;
import cn.lg.soar.system.api.enums.SysConfigTypeEnum;
import cn.lg.soar.system.biz.modules.general.entity.SysConfig;
import cn.lg.soar.system.biz.modules.general.service.ISysConfigService;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.List;

/**
 * 租户配置
 * @author luguoxiang
 * @since 2023-07-13
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
@Tag(name = "租户配置")
@Validated
@RestController
@RequestMapping(ModuleConst.MODULE_NAME + "/tenant/config")
public class TenantConfigController extends BaseController<SysConfig, ISysConfigService> {

    @Override
    protected Serializable getId(SysConfig entity) {
        return entity.getCode();
    }

    @GetMapping("{code}")
    public HttpResult<String> get(@PathVariable("code") String code) {
        return HttpResult.data(service.get(code));
    }

    @DisabledTenantAccess
    @Log(value = "设置", type = LogTypeEnum.UPDATE)
    @IdempotentByParam(value = "#p0", expire = 30_000)
    @PostMapping("set/{code}")
    public void set(@PathVariable("code") String code, @RequestBody String value) {
        service.set(code, value);
    }

    @DisabledTenantAccess
    @IdempotentByParam("#p0")
    @RequestMapping(path = "create", method = RequestMethod.POST)
    public HttpResult<Serializable> create(@RequestBody @Validated SysConfig data) {
        data.setType(SysConfigTypeEnum.other);
        return super.createResult(data);
    }

    @DisabledTenantAccess
    @RequestMapping(path = "update", method = RequestMethod.POST)
    public HttpResult<Void> update(@RequestBody @Validated SysConfig data) {
        data.setType(SysConfigTypeEnum.other);
        return super.updateResult(data);
    }

    @DisabledTenantAccess
    @RequestMapping(path = "remove", method = RequestMethod.POST)
    public HttpResult<Void> remove(@RequestBody List<String> ids) {
        service.removeOther(ids);
        return HttpResult.success();
    }

    @DisabledTenantAccess
    @RequestMapping(path = "detail", method = RequestMethod.GET)
    public HttpResult<SysConfig> detail(@RequestParam(name = "id") String id) {
        return HttpResult.data(service.getOther(id));
    }

    @DisabledTenantAccess
    @RequestMapping(path = "query", method = RequestMethod.GET)
    public HttpResult<PageResult<SysConfig>> query(PageQuery pageQuery, QueryParams params){
        params.addFilter(SysConfig::getType, ConditionEnum.eq, SysConfigTypeEnum.other.getValue());
        return super.queryResult(pageQuery, params);
    }

    @DisabledTenantAccess
    @Log(value = "导出租户配置", type = LogTypeEnum.EXPORT)
    @RequestMapping(path = "export", method = RequestMethod.GET)
    public void exports(QueryParams params, HttpServletResponse resp){
        super.exportExcel("导出", params, resp);
    }

    @Log(value = "导入租户配置", type = LogTypeEnum.IMPORT)
    @DisabledTenantAccess
    @RequestMapping(path = "import", method = RequestMethod.POST)
    public HttpResult<Void> imports(HttpServletRequest req){
        return super.importExcel(req);
    }

    @DisabledTenantAccess
    @ExistMapping
    public HttpResult<Boolean> exist(QueryParams params){
        return super.existResult(params);
    }

}

